//
//  ZJNetworkTools.m
//  Weibo29
//
//  Created by 曾静 on 16/6/30.
//  Copyright © 2016年 qq. All rights reserved.
//

#import "ZJNetworkTools.h"

@implementation ZJNetworkTools

//baseUrl,因为是静态的const的类型,所以是存在于常量区,不允许外面的进行修改
static NSString * const BaseURLString = @"https://api.weibo.com/";


//添加static的作用是将变量的作用域固定在制定的.m或者.h文件夹下面
static ZJNetworkTools *_instance;
+(instancetype)shareNetworktools{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _instance = [[self alloc]initWithBaseURL:[NSURL URLWithString:BaseURLString]];
    

        //相应的默认形式是http格式的,请求默认也是http'格式的,都是普通的二进制
        _instance.responseSerializer = [AFHTTPResponseSerializer serializer];
        _instance.responseSerializer.acceptableContentTypes = [_instance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    });
    
    return _instance;
}

- (void)NetWorkToolWithRequestType:(RequestType)type andUrlString:(NSString *)UrlString andParmers:(NSDictionary *)parmers andSuccessBlock:(SucessBlock)sucess andFailBlock:(FailBlock)fail{
   
    
    //遮罩
    if (type == GET) {
        [self GET:UrlString parameters:parmers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            if (sucess!=nil){
                //取消
                sucess(dict);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (fail) {
                fail(error);
            }
            
        }];
    } else {
        [self POST:UrlString parameters:parmers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
            if (sucess!=nil){
                //取消
                sucess(dict);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (fail) {
                fail(error);
            }
        }];
    }

    
}
//获取token
-(void)getUserToken:(NSString *)code andSuccessBlock:(SucessBlock)sucess andFailBlock:(FailBlock)fail {
    NSString * urlString = @"oauth2/access_token";
    
    NSDictionary * params = @{
                  
                  @"client_id": APPKEY,
                  @"client_secret":KAPPSECRET,
                  @"grant_type": @"authorization_code",
                  @"code": code,
                  @"redirect_uri": REDIRECT_URI
                  };
    
    
    [self NetWorkToolWithRequestType:POST andUrlString:urlString andParmers:params andSuccessBlock:sucess andFailBlock:fail];
    
}
//获取用户信息的接口
-(void)getUserInfoWithUserAccount:(ZJUserAccount*)userAccount andSuccessBlock:(SucessBlock)sucess andFailBlock:(FailBlock)fail {
    
     NSString * urlString = @"2/users/show.json";
    
    NSDictionary * params = @{
                              
                              @"access_token": userAccount.access_token,
                              
                              @"uid": userAccount.uid

                              };
    [self NetWorkToolWithRequestType:POST andUrlString:urlString andParmers:params andSuccessBlock:sucess andFailBlock:fail];
    
}
//获取首界面的信息
-(void)getHomeDataWithSinceId:(int64_t)sinceId andMaxId:(ino64_t)maxId andSuccessBlock:(SucessBlock)sucess andFailBlock:(FailBlock)fail{
    
     NSString * urlString = @"2/statuses/friends_timeline.json";
    
    //  设置参数
    NSDictionary * params = @{
                  @"since_id":[NSString stringWithFormat:@"%lld",sinceId],
                  @"max_id":[NSString stringWithFormat:@"%lld",maxId],
                  @"access_token": [ZJUserAccountTools shareAccountTools].token
                  
                  };
    
    //注意此处是一个Get请求
    [self NetWorkToolWithRequestType:GET andUrlString:urlString andParmers:params andSuccessBlock:sucess andFailBlock:fail];
    
    
}

//发布微博的接口,仅仅是文字
-(void)publicWeiBoOnlyWordWithStatus:(NSString *)status andSuccessBlock:(SucessBlock)sucess andFailBlock:(FailBlock)fail{
    NSString * urlString = @"2/statuses/update.json";
    //  设置参数
    NSDictionary * params = @{
                  @"status":status,
                  @"access_token":[ZJUserAccountTools shareAccountTools].token
                  };

    [self NetWorkToolWithRequestType:POST andUrlString:urlString andParmers:params andSuccessBlock:sucess andFailBlock:fail];
}
//发布微博的接口,文字以及一张图片
-(void)publicWordAndImageWithStatus:(NSString *)status andImageList:(NSArray *)imageList andSuccessBlock:(SucessBlock)sucess andFailBlock:(FailBlock)fail{
    
    NSString * urlString = @"2/statuses/upload.json";
    //  设置参数
    NSDictionary * params = @{
                              @"status":status,
                              @"access_token":[ZJUserAccountTools shareAccountTools].token
                              };

    [self POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (UIImage *image in imageList) {
            
            NSData *data = UIImagePNGRepresentation(image);
            
            [formData appendPartWithFileData:data name:@"pic" fileName:@"haha" mimeType:@ "application/octet-stream"];//a "application/octet-stream"允许任意的形式
        }
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        sucess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
    
}














@end
